class smx_nodejs
{
  require smx_download

  $NODE_HOME='/opt/node-v4.2.2-linux-x64'

  Exec { path => [ '/bin/', '/sbin/' , '/usr/bin/', '/usr/sbin/' ] }

  exec { 'extract-nodejs':
    creates => "$NODE_HOME",
    cwd     => '/tmp/vagrant-cache/wget/nodejs',
    command => "tar -zxf node-linux-x64.tar.gz -C /opt; $NODE_HOME/bin/npm config set prefix '/usr'",
    before  => [File['/usr/lib/node_modules'],File['/usr/bin/npm'],File['/usr/bin/node']]
  }

  file { '/usr/lib/node_modules':
    ensure => 'link',
    force  => true,
    target => "$NODE_HOME/lib/node_modules",
  }
  file { '/usr/bin/npm':
    ensure => 'link',
    target => "$NODE_HOME/bin/npm",
  }
  file { '/usr/bin/node':
    ensure => 'link',
    target => "$NODE_HOME/bin/node",
  }

  file { '/vagrant/vagrant/npm_install.sh':
    ensure => present,
    mode   => '0755',
    #      owner => root, group => root,
    source =>  "puppet:///modules/smx_nodejs/npm_install.sh",
  }

  exec { 'npm_install_files':
    cwd       => '/vagrant/vagrant',
    command   => "/vagrant/vagrant/npm_install.sh bower",
    timeout   => '1800', # half an hour, at most
    logoutput => true,
    require   => [File['/vagrant/vagrant/npm_install.sh'],File['/usr/bin/npm'],File['/usr/bin/node']]
  }

}
